package no.mehdi.game.web;

import java.io.Serializable;

import no.mehdi.game.domain.Player;

/**
 * The gameForm.
 * 
 * @author Mehdi Zare
 * 
 */
public class GameForm implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	/** The player in from */
	private Player player;
	/** The playerName */
	private String name;
	/** the password */
	private String password;
	/** Indicate if the user is logged in */
	private boolean userLoggedIn;
	/** Current creature */
	private String creature;
	/** Current game result */
	private String gameResult;
	/** indicate the waiting state */
	private boolean waiting;
	/** Start waiting time in milliseconds */
	private long startWaitingTime;
	/** Next creature due to slow rendering. */
	private String nextCreature;
	/** The counter text */
	private String counterText;

	/**
	 * Add 1 win to the current player.
	 */
	public void addWin() {
		player.addWin();
	}

	/**
	 * Add 1 loss to the current player.
	 */
	public void addLoss() {
		player.addLoss();
	}

	/**
	 * @return the win
	 */
	public int getWin() {
		return player.getWin();
	}

	/**
	 * @return the loss
	 */
	public int getLoss() {
		return player.getLoss();
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the player
	 */
	public Player getPlayer() {
		return player;
	}

	/**
	 * @param player
	 *            the player to set
	 */
	public void setPlayer(Player player) {
		this.player = player;
	}

	/**
	 * @param userLoggedIn
	 *            the userLoggedIn to set
	 */
	public void setUserLoggedIn(boolean userLoggedIn) {
		this.userLoggedIn = userLoggedIn;
	}

	/**
	 * @return the userLoggedIn
	 */
	public boolean isUserLoggedIn() {
		return userLoggedIn;
	}

	/**
	 * @param creature
	 *            the creature to set
	 */
	public void setCreature(String creature) {
		this.creature = creature;
	}

	/**
	 * @return the creature
	 */
	public String getCreature() {
		return creature;
	}

	/**
	 * @param gameResult
	 *            the gameResult to set
	 */
	public void setGameResult(String gameResult) {
		this.gameResult = gameResult;
	}

	/**
	 * @return the gameResult
	 */
	public String getGameResult() {
		return gameResult;
	}

	/**
	 * @param waiting
	 *            the waiting to set
	 */
	public void setWaiting(boolean waiting) {
		this.waiting = waiting;
	}

	/**
	 * @return the waiting
	 */
	public boolean isWaiting() {
		return waiting;
	}

	public long getStartWaitingTime() {
		return startWaitingTime;
	}

	public void setStartWaitingTime(long startWaitingTime) {
		this.startWaitingTime = startWaitingTime;
	}

	/**
	 * @return the nextCreature
	 */
	public String getNextCreature() {
		return nextCreature;
	}

	/**
	 * @param nextCreature
	 *            the nextCreature to set
	 */
	public void setNextCreature(String nextCreature) {
		this.nextCreature = nextCreature;
	}

	/**
	 * Check the waiting state is over. It will change some values 1 sec before
	 * waiting state is over. This is because of slow rendering.
	 * 
	 * @return the counterText
	 */
	public String getCounterText() {
		// Prepare the new game 1 sec before the buttons are activated
		if ((System.currentTimeMillis() - startWaitingTime) / 1000 > 9) {
			setCreature(nextCreature);
			setGameResult(null);
			setCounterText(null);
		}
		// Activate the buttons to start the new game
		if ((System.currentTimeMillis() - startWaitingTime) / 1000 > 10) {
			setWaiting(false);
		}
		// If still waiting update the counter.
		if (waiting) {
			setCounterText("New creature in " + (10 - ((System.currentTimeMillis() - startWaitingTime) / 1000)) + " seconds.");
		}
		return counterText;
	}

	/**
	 * @param counterText
	 *            the counterText to set
	 */
	public void setCounterText(String counterText) {
		this.counterText = counterText;
	}

	/**
	 * @return the password
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * @param password
	 *            the password to set
	 */
	public void setPassword(String password) {
		this.password = password;
	}

}
